package com.study.spmongodb.book.wwj.char17;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/29 10:47
 */
public class ReadLock implements Lock {

    private final ReadWriteLockImpl readWriteLock;

    public ReadLock(ReadWriteLockImpl readWriteLock) {
        this.readWriteLock = readWriteLock;
    }

    @Override
    public void lock() throws InterruptedException {
        synchronized (readWriteLock.getMUTEX()) {
            //若此时有线程正在进行写操作，或者有写线程 在等待并且偏向锁的标识为true时，就会无法获得锁，只能被挂起
            while (readWriteLock.getWritingWriters() > 0
                    || (readWriteLock.getPreferWriter()
                    && readWriteLock.getWaitingWrtiers() > 0)) {
                readWriteLock.getMUTEX().wait();
            }
            //成功获取锁，并使用  readingReaders的数量增加
        }
    }

    @Override
    public void unlock() {

    }
}
